package com.medvalley.tcm.controller;

import com.medvalley.tcm.base.Result;
import com.medvalley.tcm.service.WhiteSNService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author linsong
 * @date 2019/3/29 17:02
 **/
@RestController
@Api
public class SNController extends BaseController {

    @Autowired
    private WhiteSNService whiteSNService;

    private boolean isAdd = false;

    @ApiOperation(value = "校验是否在白名单")
    @RequestMapping(value = "/sn/validate", method = RequestMethod.GET)
    public Result validateSN(String sn) {
        if (StringUtils.isEmpty(sn)) {
            return success(0);
        }
        if (whiteSNService.findOne(sn) == null) {
            if (isAdd) {
                whiteSNService.saveOne(sn);
                return success(1);
            }
            return success(0);
        }
        return success(1);
    }

    @ApiOperation(value = "打开或关闭开关")
    @RequestMapping(value = "/sn/switch", method = RequestMethod.GET)
    public Result switchAdd(int v) {
        if (v == 0) {
            isAdd = false;
        }
        if (v == 1) {
            isAdd = true;
        }
        return success();
    }

}
